let AC_GAME_OBJECT = [];

export class Ac_Game_Object {
    constructor() {
        AC_GAME_OBJECT.push(this);
        this.has_call_start = false;
        this.timedelta = 0;
        this.uuid = this.create_uuid();
    }

    create_uuid() {
        let res = "";
        for(let i = 0; i < 8; i ++) {
            res += Math.floor(Math.random() * 10);
        }
        return res;
    }

    start(){

    }

    update() {

    }

    on_destroy() {

    }

    late_update() {

    }

    destroy(){
        this.on_destroy();
        for (let i = 0; i < AC_GAME_OBJECT.length; i ++) {
            if (AC_GAME_OBJECT[i] === this) {
                AC_GAME_OBJECT.splice(i, 1);
                break;
            }
        }
    }
}
let last_timestamp;
let AC_GAME_ANIMATION = function(timestamp){
    for(let i = 0; i < AC_GAME_OBJECT.length; i ++) {
      let obj = AC_GAME_OBJECT[i];
      if (!obj.has_call_start){
         obj.start();
         obj.has_call_start = true;
      }else {
         obj.timedelta = timestamp - last_timestamp; 
         obj.update();
      }
    }

    for(let i = 0; i < AC_GAME_OBJECT.length; i ++) {
        let obj = AC_GAME_OBJECT[i];
        obj.late_update();
    }
    last_timestamp = timestamp;
    requestAnimationFrame(AC_GAME_ANIMATION);
}
    
requestAnimationFrame(AC_GAME_ANIMATION);
